import brandList from '@/components/filter-list/brand.vue'
import carList from './components/carList.vue'
export default {
  components: {
    brandList,
    carList
  },
  data() {
    return {
      pageLoading: false,
      isHeight: 0,
      navHight: 0,
      isShow: true,
      sortTitle: '智能排序',
      loadStatus: 'nomore',
      //车源列表
      carlist: [],
      // 面包屑
      history: {
        zhineng: '',
        brand: ''
      },
      searchKey: '', //搜索历史关键字
      iconSearch: '',
      /*一成首付*/
      paymentValue: 0,
      isOpen: false,
      //1成智能排序（通用）
      paymentsSort: [{
        id: 0,
        name: '智能排序'
      },
      {
        id: 1,
        name: '降幅最高'
      }
      ],
      brandId: '',
      carIndex: 1,
    }
  },
  computed: {
    stickyHeight() {
      return Math.floor(Number(this.navHight) / (uni.upx2px(Number(this.navHight)) / Number(this.navHight))) - 1
    }
  },
  // onReady() {
  //   //获取导航栏高度

  //   const query = uni.createSelectorQuery().in(this)
  //   query.select('.nav').boundingClientRect(data => {
  //     // this.height = data.height
  //     this.navHight = data.height
  //   }).exec()
  // },
  onReachBottom(){
	  this.buyHome()
  },
  created() {
    this.buyHome()
  },
  methods: {
    more(){
      this.buyHome()
    },
    onPullDownRefresh (){
		  // this.buyList()
		  this.buyHome()

    },
    // 加载前、中、后
    moreStatus(status) {
      this.loadStatus = status
    },
    //列表数据
    async buyHome() {
      uni.showLoading({
        title: '加载中'
      })
      this.loadStatus = 'loading'
      try {
        let data = {
          'sorting': this.paymentValue,
          'pageIndex': this.carIndex,
          'pageSize': 10,
          'brandId': String(this.brandId), //品牌id
          'search': this.searchKey,
        }
        const res = await this.$api.buyCar.goodsCarSourceDropPriceList(data)

        this.carlist = [...this.carlist, ...res.data.list]

        uni.stopPullDownRefresh()
        this.$nextTick(() => {
          this.pageLoading = true
        })
        uni.hideLoading()
        if(res.data.total <= 10){
				  this.loadStatus = 'nomore'
        }else{
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')

        if (res.data.list.length === 0) return false
        this.carIndex += 1 // 当前页加1
      } catch (e) {
        uni.stopPullDownRefresh()
        uni.hideLoading()
      }
    },

    closePayment() {
      this.$refs.paymentSort.close()
    },
    // 通用品牌排序
    brandListChange(id, type) {
      this.brandId = id
      this.carIndex = 1
      this.carlist = []
      this.history.brand = type.brand
      this.buyHome()
    },
    //重置
    reset() {
      this.sortTitle = '智能排序',
      this.history = {
        zhineng: '',
        brand: '',
      }
      this.carIndex = 1
      this.carlist = []
      this.paymentValue = 0
      this.brandId = ''
      this.buyHome()
    },
    //删除历史记录
    delZhineng(e) {
      this.sortTitle = '智能排序'

      this.carIndex = 1
      this.carlist = []
      this.paymentValue = 0
      this.history.zhineng = ''

      this.buyHome()
    },
    delBrand(e) {
      this.carIndex = 1
      this.carlist = []
      this.brandId = ''
      this.history.brand = ''
      this.buyHome()
    },
    //搜索
    change(e){
		  if(e === ''){
		    this.carlist = []
		    this.carIndex = 1
		    this.buyHome()
		  }
    },
    //搜索
    onSearch(e) {
      this.searchKey = e

      if (e === '') {
        uni.showToast({
          title: '请输入关键字',
          icon: 'none',
          duration: 1000
        })
        return false
      }
      this.carIndex = 1
      this.carlist = []
      this.buyHome()
    },
    //下拉关闭
    close(e) {
      this.isOpen = false

    },
    //下拉列表打开
    open(e) {
      this.isOpen = true

    },

    // 智能排序
    paymentSort(item) {
      this.sortTitle = item.name
      this.paymentValue = item.id
      this.carIndex = 1
      this.carlist = []
      this.history.zhineng = item.name
      this.$refs.paymentSort.close()
      this.buyHome()
    },
  }
}
